package member

import (
	"log"
	"net/http"

	"awfan.cn/utils"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
)

func all(format string, w http.ResponseWriter, db *mgo.Database) {
	c := db.C(utils.PkgName(MEMBERS_XML{}))

	v := MEMBERS_XML{}

	err := c.Find(bson.M{"account": bson.M{"$ne": "root"}}).All(&v.Members)

	if err != nil {
		utils.WriteError(format, 401, err.Error(), w)
		return
	}

	for index, m := range v.Members {
		v.Members[index].Password = "******"

		var url string
		if url, err = utils.ToImageFile(m.Id.Hex(), m.Image); err != nil {
			log.Println("#convert to local image failed:", err.Error())
			continue
		}

		v.Members[index].Image = url
	}
	utils.WriteData(format, v, w)
}
